//
//  DebugAppInfoUnit.m
//  YK_DebugManager
//
//  Created by edward on 2021/2/24.
//

#import "DebugAppInfoUnit.h"
#import <sys/utsname.h>
#import <UIKit/UIKit.h>
#import <AddressBook/AddressBook.h>
#import <CoreLocation/CLLocationManager.h>
#import <AVFoundation/AVFoundation.h>
#import <AssetsLibrary/AssetsLibrary.h>
#import <Photos/Photos.h>
#import <Contacts/Contacts.h>
#import <EventKit/EventKit.h>
#include <ifaddrs.h>
#include <arpa/inet.h>
#include <net/if.h>

#define IOS_CELLULAR    @"pdp_ip0"
#define IOS_WIFI        @"en0"
//#define IOS_VPN       @"utun0"
#define IP_ADDR_IPv4    @"ipv4"
#define IP_ADDR_IPv6    @"ipv6"

@interface DebugAppInfoUnit ()
///
@property(nonatomic,strong,readwrite) NSArray *infoList;

@end

@implementation DebugAppInfoUnit

static DebugAppInfoUnit *_instance;

+ (id)allocWithZone:(struct _NSZone *)zone
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [super allocWithZone:zone];
    });

    return _instance;
}

+ (instancetype)sharedInstance
{
    if (_instance == nil) {
        _instance = [[DebugAppInfoUnit alloc] init];
    }
    return _instance;
}

- (NSArray *)infoList
{
    return @[
        @{
            @"title":@"手机信息",
            @"array":@[
                    @{
                        @"title":@"设备名称",
                        @"value":[self iphoneName]
                        },
                    @{
                        @"title":@"手机型号",
                        @"value":[self iphoneType]
                        },
                    @{
                        @"title":@"系统版本",
                        @"value":[self iphoneSystemVersion]
                        },
                    @{
                        @"title":@"手机屏幕",
                        @"value":[NSString stringWithFormat:@"%@ * %@",@(UIScreen.mainScreen.bounds.size.width).stringValue,@(UIScreen.mainScreen.bounds.size.height).stringValue]
                         },
                    @{
                        @"title":@"ipV4",
                        @"value":[self getIPAddress:YES]
                         },
                    @{
                        @"title":@"ipV6",
                        @"value":[self getIPAddress:NO]
                         }
                    ]
            },
        @{
            @"title":@"App信息",
            @"array":@[@{@"title":@"Bundle ID",
                         @"value":[self bundleIdentifier]},
                       @{@"title":@"AppName",
                         @"value":[self bundleDisplayName]},
                       @{@"title":@"Version",
                         @"value":[self bundleVersion]},
                       @{@"title":@"VersionCode",
                         @"value":[self bundleShortVersionString]},
                       @{@"title":@"Capabilities",
                         @"value":[self bundleCapabilities]
                       }].mutableCopy
            },
        @{
            @"title":@"权限信息",
            @"array":@[@{
                           @"title":@"地理位置权限",
                           @"value":[self locationAuthority]
                           },
                       @{
                           @"title":@"网络权限",
                           @"value":@"未知"
                           },
                       @{
                           @"title":@"推送权限",
                           @"value":[self pushAuthority]
                           },
                       @{
                           @"title":@"相机权限",
                           @"value":[self cameraAuthority]
                           },
                       @{
                           @"title":@"麦克风权限",
                           @"value":[self audioAuthority]
                           },
                       @{
                           @"title":@"相册权限",
                           @"value":[self photoAuthority]
                           },
                       @{
                           @"title":@"通讯录权限",
                           @"value":[self addressAuthority]
                           },
                       @{
                           @"title":@"日历权限",
                           @"value":[self calendarAuthority]
                           },
                       @{
                           @"title":@"提醒事项权限",
                           @"value":[self remindAuthority]
                           }
                       ]
            }
        ];
}

- (NSString *)appName
{
    NSString *appName = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDisplayName"];
    if (!appName) {
        appName = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleName"];
    }
    return appName;
}

- (NSString *)iphoneName
{
    return [UIDevice currentDevice].name;
}

- (NSString *)iphoneSystemVersion
{
    return [UIDevice currentDevice].systemVersion;
}

- (NSString *)bundleIdentifier
{
    return [[NSBundle mainBundle] bundleIdentifier]?:@"";
}

- (NSString *)bundleVersion
{
    return [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"]?:@"";
}

- (NSString *)bundleShortVersionString
{
    return [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]?:@"";
}

- (NSString *)bundleCapabilities
{
    NSArray *array = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"UIRequiredDeviceCapabilities"]?:@[];
    return [array componentsJoinedByString:@"/"];
}
- (NSString *)bundleDisplayName
{
    return [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDisplayName"]?:@"Unknown";
}

- (NSString *)iphoneType{
    struct utsname systemInfo;
    uname(&systemInfo);
    NSString *platform = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
    
    //iPhone
    if ([platform isEqualToString:@"iPhone1,1"]) return @"iPhone 1G";
    if ([platform isEqualToString:@"iPhone1,2"]) return @"iPhone 3G";
    if ([platform isEqualToString:@"iPhone2,1"]) return @"iPhone 3GS";
    if ([platform isEqualToString:@"iPhone3,1"]) return @"iPhone 4";
    if ([platform isEqualToString:@"iPhone3,2"]) return @"iPhone 4";
    if ([platform isEqualToString:@"iPhone4,1"]) return @"iPhone 4S";
    if ([platform isEqualToString:@"iPhone5,1"]) return @"iPhone 5";
    if ([platform isEqualToString:@"iPhone5,2"]) return @"iPhone 5";
    if ([platform isEqualToString:@"iPhone5,3"]) return @"iPhone 5C";
    if ([platform isEqualToString:@"iPhone5,4"]) return @"iPhone 5C";
    if ([platform isEqualToString:@"iPhone6,1"]) return @"iPhone 5S";
    if ([platform isEqualToString:@"iPhone6,2"]) return @"iPhone 5S";
    if ([platform isEqualToString:@"iPhone7,1"]) return @"iPhone 6 Plus";
    if ([platform isEqualToString:@"iPhone7,2"]) return @"iPhone 6";
    if ([platform isEqualToString:@"iPhone8,1"]) return @"iPhone 6S";
    if ([platform isEqualToString:@"iPhone8,2"]) return @"iPhone 6S Plus";
    if ([platform isEqualToString:@"iPhone8,4"]) return @"iPhone SE";
    if ([platform isEqualToString:@"iPhone9,1"]) return @"iPhone 7";
    if ([platform isEqualToString:@"iPhone9,3"]) return @"iPhone 7";
    if ([platform isEqualToString:@"iPhone9,2"]) return @"iPhone 7 Plus";
    if ([platform isEqualToString:@"iPhone9,4"]) return @"iPhone 7 Plus";
    if ([platform isEqualToString:@"iPhone10,1"]) return @"iPhone 8";
    if ([platform isEqualToString:@"iPhone10,4"]) return @"iPhone 8";
    if ([platform isEqualToString:@"iPhone10,2"]) return @"iPhone 8 Plus";
    if ([platform isEqualToString:@"iPhone10,5"]) return @"iPhone 8 Plus";
    if ([platform isEqualToString:@"iPhone10,3"]) return @"iPhone X";
    if ([platform isEqualToString:@"iPhone10,6"]) return @"iPhone X";
    if ([platform isEqualToString:@"iPhone11,8"]) return @"iPhone XR";
    if ([platform isEqualToString:@"iPhone11,2"]) return @"iPhone XS";
    if ([platform isEqualToString:@"iPhone11,4"]) return @"iPhone XS Max";
    if ([platform isEqualToString:@"iPhone11,6"]) return @"iPhone XS Max";
    if ([platform isEqualToString:@"iPhone12,1"]) return @"iPhone 11";
    if ([platform isEqualToString:@"iPhone12,3"]) return @"iPhone 11 Pro";
    if ([platform isEqualToString:@"iPhone12,5"]) return @"iPhone 11 Pro Max";
    if ([platform isEqualToString:@"iPhone12,8"]) return @"iPhone SE 2";
    if ([platform isEqualToString:@"iPhone13,1"]) return @"iPhone 12 mini";
    if ([platform isEqualToString:@"iPhone13,2"]) return @"iPhone 12";
    if ([platform isEqualToString:@"iPhone13,3"]) return @"iPhone 12 Pro";
    if ([platform isEqualToString:@"iPhone13,4"]) return @"iPhone 12 Pro Max";
    if ([platform isEqualToString:@"iPhone14,3"]) return @"iPhone 13 Pro Max";
    if ([platform isEqualToString:@"iPhone14,2"]) return @"iPhone 13 Pro";
    if ([platform isEqualToString:@"iPhone14,5"]) return @"iPhone 13";
    if ([platform isEqualToString:@"iPhone14,4"]) return @"iPhone 13 mini";
    
    return platform;
}

- (NSString *)getIPAddress:(BOOL)preferIPv4
{
    NSArray *searchArray = preferIPv4 ?
                            @[ /*IOS_VPN @"/" IP_ADDR_IPv4, IOS_VPN @"/" IP_ADDR_IPv6,*/ IOS_WIFI @"/" IP_ADDR_IPv4, IOS_WIFI @"/" IP_ADDR_IPv6, IOS_CELLULAR @"/" IP_ADDR_IPv4, IOS_CELLULAR @"/" IP_ADDR_IPv6 ] :
                            @[ /*IOS_VPN @"/" IP_ADDR_IPv6, IOS_VPN @"/" IP_ADDR_IPv4,*/ IOS_WIFI @"/" IP_ADDR_IPv6, IOS_WIFI @"/" IP_ADDR_IPv4, IOS_CELLULAR @"/" IP_ADDR_IPv6, IOS_CELLULAR @"/" IP_ADDR_IPv4 ] ;

    NSDictionary *addresses = [self getIPAddresses];
    __block NSString *address;
    [searchArray enumerateObjectsUsingBlock:^(NSString *key, NSUInteger idx, BOOL *stop)
        {
            address = addresses[key];
            if(address) *stop = YES;
        } ];
    return address ? address : @"0.0.0.0";
}

//获取所有相关IP信息
- (NSDictionary *)getIPAddresses
{
    NSMutableDictionary *addresses = [NSMutableDictionary dictionaryWithCapacity:8];

    // retrieve the current interfaces - returns 0 on success
    struct ifaddrs *interfaces;
    if(!getifaddrs(&interfaces)) {
        // Loop through linked list of interfaces
        struct ifaddrs *interface;
        for(interface=interfaces; interface; interface=interface->ifa_next) {
            if(!(interface->ifa_flags & IFF_UP) /* || (interface->ifa_flags & IFF_LOOPBACK) */ ) {
                continue; // deeply nested code harder to read
            }
            const struct sockaddr_in *addr = (const struct sockaddr_in*)interface->ifa_addr;
            char addrBuf[ MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN) ];
            if(addr && (addr->sin_family==AF_INET || addr->sin_family==AF_INET6)) {
                NSString *name = [NSString stringWithUTF8String:interface->ifa_name];
                NSString *type;
                if(addr->sin_family == AF_INET) {
                    if(inet_ntop(AF_INET, &addr->sin_addr, addrBuf, INET_ADDRSTRLEN)) {
                        type = IP_ADDR_IPv4;
                    }
                } else {
                    const struct sockaddr_in6 *addr6 = (const struct sockaddr_in6*)interface->ifa_addr;
                    if(inet_ntop(AF_INET6, &addr6->sin6_addr, addrBuf, INET6_ADDRSTRLEN)) {
                        type = IP_ADDR_IPv6;
                    }
                }
                if(type) {
                    NSString *key = [NSString stringWithFormat:@"%@/%@", name, type];
                    addresses[key] = [NSString stringWithUTF8String:addrBuf];
                }
            }
        }
        // Free memory
        freeifaddrs(interfaces);
    }
    return [addresses count] ? addresses : nil;
}

- (NSString *)locationAuthority{
    NSString *authority = @"";
    if ([CLLocationManager locationServicesEnabled]) {
        CLAuthorizationStatus state = [CLLocationManager authorizationStatus];
        if (state == kCLAuthorizationStatusNotDetermined) {
            authority = @"用户还没有选择";
        }else if(state == kCLAuthorizationStatusRestricted){
            authority = @"家长控制";
        }else if(state == kCLAuthorizationStatusDenied){
            authority = @"用户拒绝";
        }else if(state == kCLAuthorizationStatusAuthorizedAlways){
            authority = @"总是允许";
        }else if(state == kCLAuthorizationStatusAuthorizedWhenInUse){
            authority = @"在使用期间允许";
        }
    }else{
        authority = @"不可用";
    }
    return authority;
}

- (NSString *)pushAuthority{
    if ([[UIApplication sharedApplication] currentUserNotificationSettings].types  == UIUserNotificationTypeNone) {
        return @"NO";
    }
    return @"YES";
}

- (NSString *)cameraAuthority{
    NSString *authority = @"";
    NSString *mediaType = AVMediaTypeVideo;//读取媒体类型
    AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:mediaType];//读取设备授权状态
    switch (authStatus) {
        case AVAuthorizationStatusNotDetermined:
            authority = @"用户还没有选择";
            break;
        case AVAuthorizationStatusRestricted:
            authority = @"家长控制";
            break;
        case AVAuthorizationStatusDenied:
            authority = @"用户拒绝";
            break;
        case AVAuthorizationStatusAuthorized:
            authority = @"已授权";
            break;
        default:
            break;
    }
    return authority;
}

- (NSString *)audioAuthority{
    NSString *authority = @"";
    NSString *mediaType = AVMediaTypeAudio;//读取媒体类型
    AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:mediaType];//读取设备授权状态
    switch (authStatus) {
        case AVAuthorizationStatusNotDetermined:
            authority = @"用户还没有选择";
            break;
        case AVAuthorizationStatusRestricted:
            authority = @"家长控制";
            break;
        case AVAuthorizationStatusDenied:
            authority = @"用户拒绝";
            break;
        case AVAuthorizationStatusAuthorized:
            authority = @"已授权";
            break;
        default:
            break;
    }
    return authority;
}

- (NSString *)photoAuthority{
    NSString *authority = @"";
    #if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_8_0 //iOS 8.0以下使用AssetsLibrary.framework
    ALAuthorizationStatus status = [ALAssetsLibrary authorizationStatus];
    switch (status) {
        case ALAuthorizationStatusNotDetermined:    //用户还没有选择
        {
            authority = @"用户还没有选择";
        }
            break;
        case ALAuthorizationStatusRestricted:       //家长控制
        {
            authority = @"家长控制";
        }
            break;
        case ALAuthorizationStatusDenied:           //用户拒绝
        {
            authority = @"用户拒绝";
        }
            break;
        case ALAuthorizationStatusAuthorized:       //已授权
        {
            authority = @"已授权";
        }
            break;
        default:
            break;
    }
    #else   //iOS 8.0以上使用Photos.framework
    PHAuthorizationStatus current = [PHPhotoLibrary authorizationStatus];
    switch (current) {
        case PHAuthorizationStatusNotDetermined:    //用户还没有选择
        {
            authority = @"用户还没有选择";
        }
            break;
        case PHAuthorizationStatusRestricted:       //家长控制
        {
            authority = @"家长控制";
        }
            break;
        case PHAuthorizationStatusDenied:           //用户拒绝
        {
            authority = @"用户拒绝";
        }
            break;
        case PHAuthorizationStatusAuthorized:       //已授权
        {
            authority = @"已授权";
        }
            break;
        case PHAuthorizationStatusLimited:
        {
            authority = @"选择部分图片";
        }break;
        default:
            break;
    }
    #endif
    return authority;
}

- (NSString *)addressAuthority{
    NSString *authority = @"";
    if (@available(iOS 9.0, *)) {//iOS9.0之后
        CNAuthorizationStatus authStatus = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];
        switch (authStatus) {
            case CNAuthorizationStatusAuthorized:
                authority = @"已授权";
                break;
            case CNAuthorizationStatusDenied:
            {
                authority = @"用户拒绝";
            }
                break;
            case CNAuthorizationStatusNotDetermined:
            {
                authority = @"用户还没有选择";
            }
                break;
            case CNAuthorizationStatusRestricted:
                authority = @"家长控制";
                break;
        }
    }else{//iOS9.0之前
        ABAuthorizationStatus authorStatus = ABAddressBookGetAuthorizationStatus();
        switch (authorStatus) {
            case kABAuthorizationStatusAuthorized:
                authority = @"已授权";
                break;
            case kABAuthorizationStatusDenied:
            {
                authority = @"用户拒绝";
            }
                break;
            case kABAuthorizationStatusNotDetermined:
            {
                authority = @"用户还没有选择";
            }
                break;
            case kABAuthorizationStatusRestricted:
                authority = @"家长控制";
                break;
            default:
                break;
        }
    }
    return authority;
}

- (NSString *)calendarAuthority{
    NSString *authority = @"";
    EKAuthorizationStatus status = [EKEventStore authorizationStatusForEntityType:EKEntityTypeEvent];
    switch (status) {
        case EKAuthorizationStatusNotDetermined:
            authority = @"用户还没有选择";
            break;
        case EKAuthorizationStatusRestricted:
            authority = @"家长控制";
            break;
        case EKAuthorizationStatusDenied:
            authority = @"用户拒绝";
            break;
        case EKAuthorizationStatusAuthorized:
            authority = @"已授权";
            break;
        default:
            break;
    }
    return authority;
}

- (NSString *)remindAuthority{
    NSString *authority = @"";
    EKAuthorizationStatus status = [EKEventStore authorizationStatusForEntityType:EKEntityTypeReminder];
    switch (status) {
        case EKAuthorizationStatusNotDetermined:
            authority = @"用户还没有选择";
            break;
        case EKAuthorizationStatusRestricted:
            authority = @"家长控制";
            break;
        case EKAuthorizationStatusDenied:
            authority = @"用户拒绝";
            break;
        case EKAuthorizationStatusAuthorized:
            authority = @"已授权";
            break;
        default:
            break;
    }
    return authority;
}

@end
